﻿namespace NEnsure.Rules
{
	public class RequiredRule<T> : BaseBusinessRule<T>, IBusinessRule<T>
	{
		public override string RuleKey
		{
			get { return "Required Rule"; }
		}

		public RequiredRule()
			: base("{key} is required.")
		{

		}

		public bool IsBrokenBy(T value)
		{
			if (object.Equals(value, null))
			{
				return true; 	
			}

			if (typeof(T) == typeof(string))
			{
				return isBrokenBy(value as string);
			}

			if (value is IEmptyEquatable)
			{
				return (value as IEmptyEquatable).AmIEmpty();
			}

			return value.Equals(default(T));
		}

		private static bool isBrokenBy(string value)
		{
			if (string.IsNullOrEmpty(value))
			{
				return true;
			}

			if (string.IsNullOrEmpty(value.Trim()))
			{
				return true;
			}

			return false;
		}
	}
}
